import { cloneDeep } from "lodash-es";
import { isUrl } from "@/utils/is";
export const getRawRoute = (route) => {
  if (!route) return route;
  const { matched, ...opt } = route;
  return {
    ...opt,
    matched: matched
      ? matched.map((item) => ({
          meta: item.meta,
          name: item.name,
          path: item.path,
        }))
      : undefined,
  };
};
/**
 *
 * @param {string} parentPath
 * @param {string} path
 * @returns
 */
export const pathResolve = (parentPath, path) => {
  if (isUrl(path)) return path;
  const childPath = path.startsWith("/") || !path ? path : `/${path}`;
  return `${parentPath}${childPath}`.replace(/\/\//g, "/").trim();
};
// 路由降级
export const flatMultiLevelRoutes = (routes) => {
  const modules = cloneDeep(routes);
  for (let index = 0; index < modules.length; index++) {
    const route = modules[index];
    if (!isMultipleRoute(route)) {
      continue;
    }
    promoteRouteLevel(route);
  }
  return modules;
};
